package com.e0box.game.display
{
	import com.e0box.game.logicGraphics.Game;
	import com.e0box.game.logicGraphics.Options;
	
	import starling.display.Button;
	import starling.display.Image;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.text.TextField;
	import starling.utils.HAlign;
	import starling.utils.VAlign;
	
	public class MessageBox extends Sprite
	{
		private static var _instance:MessageBox;
		
		private var _bg:Image;
		private var _close:Button;
		
		private var _confirm:Button;
		private var _cancel:Button;
		
		private var _yes:Button;
		private var _no:Button;
		
		private var _title:TextField;
		private var _msg:TextField;
		
		private var _confirmFun:Function;
		private var _cancelFun:Function;
		private var _yesFun:Function;
		private var _noFun:Function;
		
		private var _confirmParam:Array;
		private var _cancelParam:Array;
		private var _yesParam:Array;
		private var _noParam:Array;
		
		public function MessageBox(single:validate)
		{
			if(!single) throw new Error("Can't instance the static class MessageBox,use 'show' or 'hide'.");
			
			init();
		}
		
		private function init():void{
			_bg = new Image(Game.instance.assets.getTexture(Options.MESSAGE_BG));
			
			_close = new Button(Game.instance.assets.getTexture(Options.MESSAGE_CLOSE));
			_confirm = new Button(Game.instance.assets.getTexture(Options.MESSAGE_CONFIRM));
			_cancel = new Button(Game.instance.assets.getTexture(Options.MESSAGE_CANCEL));
			_yes = new Button(Game.instance.assets.getTexture(Options.MESSAGE_YES));
			_no = new Button(Game.instance.assets.getTexture(Options.MESSAGE_NO));
			
			addChild(_bg);
			
			_title = new TextField(_bg.width, 140,"");
			
			_title.fontSize = 42;
			
			_title.color = 0x965a32;
			_title.hAlign = HAlign.CENTER;
			_title.vAlign = VAlign.CENTER;
			
			_msg = new TextField(_bg.width - 30, _bg.height - _title.height - _title.y - 120,"");
			_msg.color = 0x965a32;
			_msg.fontSize = 36;
			
			_msg.hAlign = HAlign.CENTER;
			_msg.vAlign = VAlign.CENTER;
			
			/*_title.border = true;
			_msg.border = true;*/
			
			_title.autoScale = true;
			_msg.autoScale = true;
			
			_title.x = 0;
			_title.y = 60;
			
			_msg.x = 15;
			_msg.y = _title.y + _title.height - 50;
			
			addChild(_title);
			addChild(_msg);
			
			_close.x = _bg.width - _close.width - 10;
			_close.y = -_close.height / 7 * 4;
			addChild(_close);
			
			_confirm.x = (_bg.width - _confirm.width - _cancel.width - 80) / 2;
			_confirm.y = _bg.height - 45;
			
			_cancel.x = _confirm.x + _confirm.width + 80;
			_cancel.y = _confirm.y;
			
			_yes.x = _confirm.x;
			_yes.y = _confirm.y;
			
			_no.x = _cancel.x;
			_no.y = _cancel.y;
			
			addChild(_confirm);
			addChild(_cancel);
			
			addChild(_yes);
			addChild(_no);
			
			_confirm.visible = _cancel.visible = _yes.visible = _no.visible = false;
			
			_confirm.addEventListener(Event.TRIGGERED, onConfirm);
			_cancel.addEventListener(Event.TRIGGERED, onCancel);
			_yes.addEventListener(Event.TRIGGERED, onYes);
			_no.addEventListener(Event.TRIGGERED, onNo);
			
			_close.addEventListener(Event.TRIGGERED, onClose);
		}
		
		
		
		private function onConfirm(evt:Event):void
		{
			if(_confirmFun){
				_confirmFun.apply(null,_confirmParam);
			}else{
				MessageBox.hide();
			}
		}
		private function onCancel(evt:Event):void
		{
			
			if(_cancelFun){
				_cancelFun.apply(null,_cancelParam);
			}else{
				MessageBox.hide();
			}
		}
		private function onYes(evt:Event):void
		{
			if(_yesFun){
				_yesFun.apply(null,_yesParam);
			}else{
				MessageBox.hide();
			}
		}
		private function onNo(evt:Event):void
		{
			if(_noFun){
				_noFun.apply(null,_noParam);
			}else{
				MessageBox.hide();
			}
		}
		
		private function onClose(evt:Event):void
		{
			MessageBox.hide();
		}
		
		public static function showWithConfirm(title:String, msg:String,showClose:Boolean = false, confirmFun:Function = null, confirmParams:Array = null, cancelFun:Function = null, cancelParams:Array = null):void{
			if(!_instance) _instance = new MessageBox(new validate());
			
			//Game.instance.addChild(_instance._mask);
			Game.instance.showMask();
			Game.instance.addChild(_instance);
			
			
			_instance._title.text = title + "\n";
			_instance._msg.text = msg;
			
			_instance.x = (Options.STAGE_WIDTH - _instance.width) >> 1;
			_instance.y = (Options.STAGE_HEIGHT - _instance.height) >> 1;
			
			_instance._confirmFun = confirmFun;
			_instance._confirmParam = confirmParams;
			
			_instance._close.visible = showClose;
			
			_instance._cancelFun = cancelFun;
			_instance._cancelParam = cancelParams;
			
			_instance._cancel.visible = _instance._confirm.visible = true;
			_instance._yes.visible = _instance._no.visible = false;
			
			_instance._confirm.x = (_instance._bg.width - _instance._confirm.width - _instance._cancel.width - 30) / 2;
			
		}
		
		public static function showWithYes(title:String, msg:String,showClose:Boolean = false, yesFun:Function = null, yesParams:Array = null, noFun:Function = null, noParams:Array = null):void{
			if(!_instance) _instance = new MessageBox(new validate());
			
			Game.instance.showMask();
			Game.instance.addChild(_instance);
			
			_instance._title.text = title + "\n";
			_instance._msg.text = msg;
			
			_instance.x = (Options.STAGE_WIDTH - _instance.width) >> 1;
			_instance.y = (Options.STAGE_HEIGHT - _instance.height) >> 1;
			
			_instance._yesFun = yesFun;
			_instance._yesParam = yesParams;
			
			_instance._noFun = noFun;
			_instance._noParam = noParams;
			
			_instance._close.visible = showClose;
			
			_instance._cancel.visible = _instance._confirm.visible = false;
			_instance._yes.visible = _instance._no.visible = true;
			
		}
		
		public static function showMessage(title:String, msg:String,showClose:Boolean = false, onHide:Function = null, onHideParams:Array = null):void{
			
			if(!_instance) _instance = new MessageBox(new validate());
			
			Game.instance.showMask();
			Game.instance.addChild(_instance);
			
			_instance._title.text = title + "\n";
			_instance._msg.text = msg;
			
			_instance.x = (Options.STAGE_WIDTH - _instance.width) >> 1;
			_instance.y = (Options.STAGE_HEIGHT - _instance.height) >> 1;
			
			_instance._confirmFun = onHide;
			_instance._confirmParam = onHideParams;
			
			_instance._confirm.visible = true;
			
			_instance._cancel.visible = false;
			_instance._yes.visible = _instance._no.visible = false;
			
			_instance._close.visible = showClose;
			
			_instance._confirm.x = (_instance._bg.width - _instance._confirm.width) / 2;
		}
		
		public static function hide():void{
			_instance.removeFromParent(false);
			Game.instance.removeMask();
			/*if(_instance && (_instance.parent is Game)){
				_instance.removeFromParent(false);
				Game.instance.removeChild(_instance);
			}*/
		}
		
	}
}

class validate{
	public function validate(){
		
	}
}